import 'dart:ffi';

import 'package:abc/view/GradientButton.dart';
import 'package:flutter/material.dart';

// 调整后的公共卡片/弹窗控件，适用于 Bottom Sheet
class CustomActionCard extends StatefulWidget {
  // 保持与之前相同的参数定义，以保持通用性
  final Widget iconWidget;
  final String title;
  final Widget contentWidget;
  final String primaryButtonText;
  final VoidCallback onPrimaryButtonPressed;
  final Widget? footerNotes;
  final VoidCallback? onClose; // 用于关闭按钮，在 Bottom Sheet 中常用于 dismiss
  final bool isShowTextFild;
  final TextEditingController? textController; // 添加文本控制器参数
  final bool isButtonEnabled; // 添加按钮启用状态参数

  const CustomActionCard({
    super.key,
    required this.iconWidget,
    required this.title,
    required this.contentWidget,
    required this.primaryButtonText,
    required this.onPrimaryButtonPressed,
    this.footerNotes,
    this.onClose,
    this.textController,
    this.isShowTextFild = false,
    this.isButtonEnabled = true, // 默认按钮启用
  });

  @override
  CustomActionCardState createState() => CustomActionCardState();
}

class CustomActionCardState extends State<CustomActionCard> {
  bool _isButtonEnabled = true;
  late TextEditingController _textController;

  @override
  void initState() {
    super.initState();
    // 使用传入的控制器或创建新的控制器
    _textController = widget.textController ?? TextEditingController();
    // 添加监听器来监听文本变化
    _textController.addListener(_onTextChanged);
    // 初始化按钮状态
    _isButtonEnabled = widget.isButtonEnabled;
  }

  @override
  void dispose() {
    // 只有在我们创建了控制器的情况下才释放它
    if (widget.textController == null) {
      _textController.dispose();
    } else {
      _textController.removeListener(_onTextChanged);
    }
    super.dispose();
  }

  void _onTextChanged() {
    // 只有当按钮启用时才根据文本内容更新按钮状态
    if (widget.isButtonEnabled) {
      setState(() {
        _isButtonEnabled = _textController.text.isNotEmpty;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    // 确定按钮的最终启用状态
    bool buttonEnabled;
    if (widget.isShowTextFild) {
      buttonEnabled =
          widget.isButtonEnabled &&
          _isButtonEnabled &&
          !_textController.text.isEmpty;
    } else {
      buttonEnabled = widget.isButtonEnabled && _isButtonEnabled;
    }

    return Container(
      // 这是 Bottom Sheet 的容器，可以设置内边距
      padding: const EdgeInsets.only(top: 22, bottom: 32, left: 14, right: 14),
      // 底部安全区域处理，防止按钮被手势或系统UI遮挡
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min, // 关键：让 Bottom Sheet 紧贴内容高度
          children: <Widget>[
            // 1. 顶部关闭按钮 (如果需要)
            if (widget.onClose != null)
              Align(
                alignment: Alignment.topRight,
                child: InkWell(
                  onTap: widget.onClose,
                  child: Image.asset(
                    'assets/images/ic_close.png',
                    width: 18,
                    height: 18,
                  ),
                ),
              ),

            // 2. 图标和标题
            Padding(
              // 如果没有关闭按钮，则给顶部留出更多空间
              padding: EdgeInsets.only(
                bottom: 16.0,
                top: widget.onClose == null ? 10.0 : 0.0,
              ),
              child: Column(
                children: [
                  widget.iconWidget,
                  const SizedBox(height: 24),
                  Text(
                    widget.title,
                    style: const TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                      color: Color(0xFF333333),
                    ),
                  ),
                ],
              ),
            ),

            // 3. 核心内容区域 - 当按钮不可用时禁用输入框
            AbsorbPointer(
              absorbing:
                  !(widget.isButtonEnabled &&
                      _isButtonEnabled), // 当按钮不可用时吸收触摸事件，禁用输入框
              child: widget.contentWidget,
            ),

            // 5. 底部小字说明
            if (widget.footerNotes != null)
              Container(
                width: double.infinity,
                child: Padding(
                  padding: const EdgeInsets.only(top: 15.0),
                  child: widget.footerNotes,
                ),
              ),

            SizedBox(height: widget.footerNotes == null ? 87 : 43),

            // 4. 主要按钮 (填充宽度)
            SizedBox(
              width: double.infinity,
              child: GradientButton(
                text: widget.primaryButtonText,
                onPressed: buttonEnabled ? widget.onPrimaryButtonPressed : null,
                gradientColors: buttonEnabled
                    ? const [Color(0xFFA877F4), Color(0xFF5650D6)]
                    : const [Color(0xFFE1E1E1), Color(0xFFE1E1E1)],
                margin: EdgeInsetsGeometry.all(0),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
